package com.zzyl.serve.service.impl;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.serve.domain.Bed;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.domain.Elder;
import com.zzyl.serve.dto.AlertDataDto;
import com.zzyl.serve.dto.HandleAlertDataDto;
import com.zzyl.serve.mapper.BedMapper;
import com.zzyl.serve.mapper.DeviceMapper;
import com.zzyl.serve.mapper.ElderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.AlertDataMapper;
import com.zzyl.serve.domain.AlertData;
import com.zzyl.serve.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

import static com.zzyl.common.utils.SecurityUtils.getUserId;
import static com.zzyl.common.utils.SecurityUtils.getUsername;

/**
 * 报警数据Service业务层处理
 * 
 * @author iteheima
 * @date 2025-03-18
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;
    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataDto alertDataDto) {
        Long userId = getUserId();
        LocalDateTime startTime = alertDataDto.getStartTime() == null ? null : LocalDateTimeUtil.of(alertDataDto.getStartTime());
        LocalDateTime endTime = alertDataDto.getEndTime() == null ? null : LocalDateTimeUtil.of(alertDataDto.getEndTime());
        List<AlertData> list = alertDataMapper.selectAlertDataListByDto(alertDataDto.getId(), alertDataDto.getStatus(), alertDataDto.getDeviceName(), userId, startTime, endTime);

//        list.forEach(alertData -> {
//            LambdaQueryWrapper<Device> eq = new LambdaQueryWrapper<Device>()
//                    .eq(Device::getIotId, alertData.getIotId())
//                    .eq(Device::getDeviceName, alertData.getDeviceName());
//            Device device = deviceMapper.selectOne(eq);
//            alertData.setNickName(device.getNodeId());
//        });
        return list;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public void handleAlertDate(HandleAlertDataDto handleAlertDataDto) {
        AlertData alertData = alertDataMapper.selectAlertDataById(handleAlertDataDto.getId());
//        Long userId = getUserId();
        alertData.setProcessingResult(handleAlertDataDto.getProcessingResult());
        alertData.setProcessingTime(LocalDateTime.now());
        alertData.setProcessorName(getUsername());
        alertData.setProcessorId(getUserId());
        alertDataMapper.handleAlertDate(alertData);
    }
}
